/*
 * Copyright (c) 2023, 2024,yzc.cn All rights reserved.
 *
 */
package com.by.mq.consumer;

import com.by.common.utils.BytesUtils;
import com.by.mapper.PurApplicationFollowupMapper;
import com.by.pojo.PurApplicationFollowup;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * <p>Project: erp-cloud-api - FollowMQConsumer</p>
 * <p>Powered by yzc On 2024-01-11 14:22:28</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */
@Component
public class FollowMQConsumer {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    PurApplicationFollowupMapper purApplicationFollowupMapper;

    /**
     * 监听
     * @param message
     * @param channel
     * @throws IOException
     */
    @RabbitListener(queues = {"followQueue"})
    public void handlerFollow(Message message, Channel channel) throws IOException {

        //将消息队列中的消息转换为对象
        Object object = BytesUtils.getObject(message.getBody());
        //将对象转为HashMap
        HashMap<String,String> map = (HashMap<String,String>) object;
        PurApplicationFollowup followup =new PurApplicationFollowup();
        followup.setApplicationCode(map.get("applicationCode"));
        followup.setApplicationSubject(map.get("applicationSubject"));
        followup.setFollowStatus(map.get("followStatus"));
            //保存到采购跟进表中
            int rs = purApplicationFollowupMapper.insert(followup);
            if (rs == 1){
                //保存成功
                //手动应答 确认消息成功处理
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            }else {
                //保存失败
                //拒绝消息或重新投递
                channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            }




    }

}
